package ro.pub.cs.hwchecker.comm.client.json;

import java.util.Date;

import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONValue;

import ro.pub.cs.hwchecker.comm.client.data.Archive;
import ro.pub.cs.hwchecker.comm.client.data.Homework;
import ro.pub.cs.hwchecker.comm.client.data.StudentSummary;
import ro.pub.cs.hwchecker.comm.client.req.ApplicationRequestBuilder;
import ro.pub.cs.hwchecker.comm.client.req.CheckerCallback;
import ro.pub.cs.hwchecker.comm.client.req.CheckerRequest;
import ro.pub.cs.hwchecker.comm.client.req.CheckerRequestException;

public class JSONApplicationRequestBuilder extends JSONRequestBuilder implements ApplicationRequestBuilder {
	private static final String baseURL = "/vmchecker/";
	
	private static final String homeworkURL = "homework";
	
	private Homework[] parseHomework(JSONValue hw) {
		Homework[] result = null;
		
		try {
			JSONArray jsonArray = hw.isArray();
			
			result = new Homework[jsonArray.size()];
			
			for (int i = 0; i < jsonArray.size(); i++) {
				JSONObject crtHW = jsonArray.get(i).isObject();
				
				result[i] = new Homework();
				
				result[i].id = crtHW.get("id").isString().stringValue();

				if (crtHW.containsKey("shortname"))
					result[i].shortName = crtHW.get("shortname").isString().stringValue();
				
				if (crtHW.containsKey("longname"))
					result[i].longName = crtHW.get("longname").isString().stringValue();
				
				if (crtHW.containsKey("description"))
					result[i].description = crtHW.get("description").isString().stringValue();
				
				if (crtHW.containsKey("duedate"))
					result[i].dueDate = new Date(
							crtHW.get("duedate").isString().stringValue());
				
				if (crtHW.containsKey("maxgrade"))
					result[i].maxGrade = Integer.parseInt(
							crtHW.get("maxgrade").isString().stringValue());
				
				if (crtHW.containsKey("testcount"))
					result[i].testCount = Integer.parseInt(
							crtHW.get("testcount").isString().stringValue());
				
			}
		} catch (NullPointerException e) {
			result = null;
		}
		
		return result;
	}
	
	public CheckerRequest performHomeworkListRequest(final CheckerCallback<Homework[]> callback) 
				throws CheckerRequestException {
		String postData = ""; // Nothing to send
		
		CheckerRequest checkerReq = performRequest(baseURL + homeworkURL, postData, 
				new CheckerCallback<JSONValue>() {

			public void onCheckerError(CheckerRequest req,
					CheckerRequestException error) {
				// Just propagate the error
				callback.onCheckerError(req, error);
			}

			public void onCheckerResponse(CheckerRequest req, JSONValue response) {
				Homework[] hw = parseHomework(response);
				
				if (hw == null)
					callback.onCheckerError(req, CheckerRequestException.errorInvalidRequest());
				else
					callback.onCheckerResponse(req, hw);
			}
		});
		
		return checkerReq;
	}

	public CheckerRequest performArchiveRequest(String arID,
			CheckerCallback<Archive> callback) throws CheckerRequestException {
		// TODO Auto-generated method stub
		return null;
	}

	public CheckerRequest performHomeworkRequest(String hwID,
			CheckerCallback<Archive[]> callback) throws CheckerRequestException {
		// TODO Auto-generated method stub
		return null;
	}

	public CheckerRequest performLogRequest(String arID, int startingFrom,
			CheckerCallback<String> callback) throws CheckerRequestException {
		// TODO Auto-generated method stub
		return null;
	}

	public CheckerRequest performStudentSummaryRequest(
			CheckerCallback<StudentSummary> callback)
			throws CheckerRequestException {
		// TODO Auto-generated method stub
		return null;
	}
}
